package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysDeptDo;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysDeptServiceImpl implements ISysDeptService {
    private final SysDeptMapper sysDeptMapper;

    /**
     * 查询部门
     */
    @Override
    public SysDeptDo queryById(Long deptId) {
        return sysDeptMapper.selectById(deptId);
    }

    /**
     * 查询部门列表
     */
    @Override
    public TableDataInfo<SysDeptDo> queryPageList(SysDeptDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDeptDo> lqw = buildQueryWrapper(entity);
        Page<SysDeptDo> result = sysDeptMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询部门列表
     */
    @Override
    public List<SysDeptDo> queryList(SysDeptDo entity) {
        LambdaQueryWrapper<SysDeptDo> lqw = buildQueryWrapper(entity);
        return sysDeptMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysDeptDo> buildQueryWrapper(SysDeptDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysDeptDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(entity.getParentId() != null, SysDeptDo::getParentId, entity.getParentId());
        lqw.eq(StringUtils.isNotBlank(entity.getAncestors()), SysDeptDo::getAncestors, entity.getAncestors());
        lqw.like(StringUtils.isNotBlank(entity.getDeptName()), SysDeptDo::getDeptName, entity.getDeptName());
        lqw.eq(entity.getOrderNum() != null, SysDeptDo::getOrderNum, entity.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(entity.getLeader()), SysDeptDo::getLeader, entity.getLeader());
        lqw.eq(StringUtils.isNotBlank(entity.getPhone()), SysDeptDo::getPhone, entity.getPhone());
        lqw.eq(StringUtils.isNotBlank(entity.getEmail()), SysDeptDo::getEmail, entity.getEmail());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysDeptDo::getStatus, entity.getStatus());
        return lqw;
    }

    /**
     * 新增部门
     */
    @Override
    public Boolean insert(SysDeptDo entity) {
        SysDeptDo add = BeanUtil.toBean(entity, SysDeptDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysDeptMapper.insert(add) > 0;
        if (flag) {
            entity.setDeptId(add.getDeptId());
        }
        return flag;
    }

    /**
     * 修改部门
     */
    @Override
    public Boolean updateById(SysDeptDo entity) {
        SysDeptDo update = BeanUtil.toBean(entity, SysDeptDo.class);
        validEntityBeforeSave(update);
        return sysDeptMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysDeptDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除部门
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysDeptMapper.deleteBatchIds(ids) > 0;
    }
}
